// pages/xuexitong/presign/presign.js
// 在页面中定义激励视频广告
let videoAd = null
const {
    xxtUrl,
    javaUrl,
    qiangguoUrl
} = require("../../../utils/util")



Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: 0,
        phone: '',
        array:[] ,
        index:0,
        signtimes:0
    },
    bindChange(e) {
        const val = e.detail.value
        this.setData({
          year: this.data.years[val[0]],
          month: this.data.months[val[1]],
          day: this.data.days[val[2]],
          isDaytime: !val[3]
        })
      },

    onShareAppMessage() {
        return {
          title: 'picker-view',
          path: 'page/component/pages/picker-view/picker-view'
        }
      },
    onChange(event) {
        console.log('radio', event.detail);
        this.setData({
            time: event.detail.value
        })
    },
    presign: function () {
        // rewardedVideoAd.show()
        //     .catch(() => {
        //         rewardedVideoAd.load()
        //             .then(() => rewardedVideoAd.show())
        //             .catch(err => {
        //                 console.log('激励视频 广告显示失败')
        //             })
        //     })
        var that = this
        if (that.data.index == 0) {
            wx.showToast({
                title: '时长为零，请重新选择',
                icon: 'error'
            })
            return
        }
            wx.showLoading({
              title: '正在加载',
            })
        wx.request({
            url: `${qiangguoUrl}/monitor/start`,
            method: 'POST',
            data: {
                phone: this.data.phone,
                uf: that.data.user.uf,
                _d: that.data.user._d,
                vc3: that.data.user.vc3,
                uid: that.data.user.UID,
                fid: that.data.user.fid,
            },
            success(res) {
                console.log("/monitor/start", res)
                wx.request({
                    url: javaUrl + '/xxt/chedule/setSchedule',
                    method: 'POST',
                    data: {
                        phone: that.data.phone,
                        time: that.data.index
                    }
                })
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success'
                })
                if(res.data.msg!='Already started'){
                      //删除时长
                const usetime=wx.getStorageSync('preSignCount')-that.data.index
                wx.setStorageSync('preSignCount', usetime)
                const array = []
                for (let i = 0; i <=wx.getStorageSync('preSignCount'); i++) {
                    array.push(i)
                }  
              that.setData({
                  signtimes: wx.getStorageSync('preSignCount'),
                  array:array
              })
                }
              
            }
        })
    },
    getPreSign() {
        wx.navigateTo({
            url: '../preSignCount/preSignCount',
        })
        // var that =this
        // videoAd.show()
        //     .catch(() => {
        //         videoAd.load()
        //             .then(() => videoAd.show())
        //             .catch(err => {
        //                 wx.showToast({
        //                     title: '激励视频 广告显示失败',
        //                     icon: 'error'
        //                 })
        //                 console.log('激励视频 广告显示失败')
        //             })
        //     })
        //     videoAd.onClose(res => {
        //         // 用户点击了【关闭广告】按钮
        //         if (res && res.isEnded) {
        //             console.log("看完了")
        //             const times=wx.getStorageSync('signtimes')
        //               wx.setStorageSync('signtimes', times+3)
        //               const array = []
        //               for (let i = 0; i <=times+3; i++) {
        //                   array.push(i)
        //               }  
        //             that.setData({
        //                 signtimes: wx.getStorageSync('signtimes'),
        //                 array:array
        //             })
        //             wx.showToast({
        //               title: '已获得时长',
        //             })
        //           // 正常播放结束，可以下发游戏奖励
        //         } else {
        //           // 播放中途退出，不下发游戏奖励
        //         }
        //     })
    },
    bindPickerChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          index: e.detail.value
        })
      },

    presignstop() {

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that=this
  
      if (wx.getStorageSync('preSignCount')=='') {
        wx.setStorageSync('preSignCount', 0)
        that.setData({
          signtimes:wx.getStorageSync('preSignCount')
        })
      }
      const array = []
      for (let i = 0; i <=wx.getStorageSync('preSignCount'); i++) {
          array.push(i)
      }  
        this.setData({
            user: wx.getStorageSync('signup'),
            phone: wx.getStorageSync('phone'),
            password:wx.getStorageSync('pwd'),
            array:array
        })

        wx.request({
            
            url: qiangguoUrl + '/login',
            method: 'POST',
            data: {
                phone: that.data.phone,
                password: that.data.password
            },
            success(res) {
                console.log("denglu", res)
                wx.setStorageSync('signup', res.data)
                that.setData({
                    user: res.data,

                })

            }

        })
       // 在页面onLoad回调事件中创建激励视频广告实例
if (wx.createRewardedVideoAd) {
    videoAd = wx.createRewardedVideoAd({
      adUnitId: 'adunit-5aad7555515c9fcd'
    })
    videoAd.onLoad(() => {})
    videoAd.onError((err) => {})
 
  }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        const array = []
        for (let i = 0; i <=wx.getStorageSync('preSignCount'); i++) {
            array.push(i)
        }  
        this.setData({
            array:array
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this
        wx.request({
            url: 'https://www.lovegcu.xyz/xxt/chedule/getSchedule',
            method: 'GET',
            success(res) {
                console.log(res)
                that.setData({
                    schedule: res.data
                })
                // for(var i=0 ;i>that.data.schedule.length;i++){
                //     console.log("i:",that.data.schedule[i])
                // }
                for (var item of that.data.schedule) {
                    console.log(item)
                    console.log(item.phone)
                    wx.request({
                        url: qiangguoUrl + '/monitor/stop',
                        method: 'POST',
                        data: {
                            phone: item.phone
                        },
                        success(res) {
                            console.log(res)

                            wx.request({
                                url: javaUrl + '/xxt/chedule/removeByPhone/' + item.phone,
                                method: 'GET',

                                success(res) {
                                    console.log("删除", res)
                                }
                            })
                        }
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})